package utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import java.time.Duration;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.ElementOption;
import io.appium.java_client.touch.offset.PointOption;

public class BaseAction {
	public AndroidDriver<AndroidElement> driver;
	public AndroidTouchAction action;

	public BaseAction(AndroidDriver<AndroidElement> driver) {
		this.driver = driver;
		action = new AndroidTouchAction(driver);
	}

	public String getToastMsg() {	
		return	this.driver.findElementByClassName("android.widget.Toast")
				.getText();
		 
	}

	/**
	 * @param by
	 * @throws InterruptedException 
	 */
	public void click(By by) throws InterruptedException {
		driver.findElement(by).click();
		Thread.sleep(1000);
	}
	
	public void click(WebElement elemnet) throws InterruptedException {
		elemnet.click();
		Thread.sleep(1000);
	}
	
	//长按
    public void longPress(AndroidElement ele){
		TouchAction ta = new TouchAction(driver);
		ta.longPress(new LongPressOptions().withElement(ElementOption.element(ele))).release().perform();
    }

  //坐标点的滑动
    public void swip(PointOption fromOption, PointOption toOption){
  		action.press(fromOption)
			.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
			.moveTo(toOption)
			.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
			.release()
			.perform();
    }
    //元素间的滑动
    public void swip(WebElement fromEle, WebElement toEle){

		
		PointOption fromOption=PointOption.point(fromEle.getLocation().getX(),fromEle.getLocation().getY());
		PointOption toOption=PointOption.point(toEle.getLocation().getX(),toEle.getLocation().getY());
		swip(fromOption,toOption);
    }
	//拖动
	
	public void longClickAndSwip(AndroidElement element) {
	
		LongPressOptions longPressOptions = LongPressOptions.longPressOptions();
		longPressOptions.withElement(ElementOption.element(element));
		
		action.longPress(longPressOptions)
			.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
			.moveTo(PointOption.point(100, element.getLocation().getY()))
			.waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
			.release()
			.perform();
	}
	
	//填内容
	public void setValue(By by, String value) throws InterruptedException {
		driver.findElement(by).clear();
		driver.findElement(by).sendKeys(value);
		Thread.sleep(1000);
	}
	
	public void setValue(AndroidElement element, String value) throws InterruptedException {
		element.clear();
		element.sendKeys(value);
		Thread.sleep(1000);
	}
	
	public void type(WebElement element,String context) throws InterruptedException{
        element.sendKeys(context);
        Thread.sleep(1000);
	}
	//回车
//	public void enter() {
//		((Object) driver).press_keycode(66);
//	}

}
